<?php

use App\Libs\Rakuten\Common\RakutenClient;
use App\Logic\Amazon\Api\CustomerOutbound;
use App\Logic\Amazon\Api\RequiredConfig;
use App\Logic\Plat\Rakuten\Overseas\GuCang\Order\OverseasOrder;
use App\Logic\Plat\Shopify\GrabCommon;
use App\Logic\SalesAmazon\Sales2AmountDaily;
use App\Logic\SalesAmazon\SalesAmount2Daily;
use App\Logic\SalesAmazon\SalesAmountDaily;
use App\Logic\SalesAmazon\SalesAmountMonthly;
use App\Logic\SalesAmazon\SalesVolume;
use App\Logic\SalesReport\DailySaleAmount;
use App\Logic\SalesReport\DailySaleVolume;

use App\Models\Basics\AmazonAccount;
use Illuminate\Support\Facades\Artisan;
use App\Crontab\Minute\SyncSsBasic;
use App\Crontab\Daily\SalesAmazonWork;
use App\Crontab\Daily\SalesReportWork;
use App\Crontab\Daily\SyncProfitSku;
use App\Crontab\Daily\SyncSsBasic as DSyncSsBasic;
use App\Crontab\Daily\SyncSSku;
use App\Crontab\Daily\SyncStocking;
use App\Crontab\Weekly\SyncProfitSku as WSyncProfitSku;
use App\Crontab\Monthly\SyncProfitSku as MSyncProfitSku;
use App\Crontab\Monthly\SyncSsBasic as MSyncSsBasic;
use App\Crontab\Monthly\TruncateTable;
use App\Logic\SalesReport\PaymentClassifyUp;
use App\Crontab\Daily\Crawl;
use SellingPartnerApi\Api\FbaOutboundV20200701Api as FbaCustomerOutboundApi;

/*
|--------------------------------------------------------------------------
| Console Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of your Closure based console
| commands. Each Closure is bound to a command instance allowing a
| simple approach to interacting with each command's IO methods.
|
*/
//Minute
//更新备货提货非FBA库存数据
Artisan::command('ss:not-fba-stock', function () {
    SyncSsBasic::syncNotFBAStock();
})->describe('syncNotFBAStock');

//Daily
//统计平台订单销量到销量表(平台)
Artisan::command('sa:volume {days=1}', function ($days) {
    SalesAmazonWork::initVolume($days);  //平台日销售量(默认1天)
})->describe('volume');
Artisan::command('sa:volume-month', function () {
    SalesAmazonWork::initVolumeMonth();  //平台月销售量
})->describe('volumeMonth');
Artisan::command('sa:amount {days=7}', function ($days) {
    SalesAmazonWork::initAmount($days);  //平台日销售额 (默认7天，倒推平台销售额数据)
})->describe('amount');
//Artisan::command('sa:amount-2', function () {
//    Sales2AmountDaily::init();
//})->describe('amount');
//Artisan::command('sa:amount-3', function () {
//    SalesAmount2Daily::init();
//})->describe('amount');
Artisan::command('sa:amount-month', function () {
    SalesAmazonWork::initAmountMonth(); //平台月销售额
})->describe('amountMonth');

//统计ERP报表(ERP)
Artisan::command('sr:cdsv {days=1}', function ($days) {
    SalesReportWork::initCDSV($days); //erp日销售量
})->describe('csvcDaily');
Artisan::command('sr:dsv', function () {
    SalesReportWork::initDSV();     //erp近30天销量（品类SKU）
})->describe('svDaily');
Artisan::command('sr:dsv-c', function () {
    SalesReportWork::initDSVC();    //erp近30天销量（品类站点）
})->describe('svcDaily');
Artisan::command('sr:msv', function () {
    SalesReportWork::initMSV();     //erp近14个月销售量（月目标）
})->describe('svMonthly');
Artisan::command('sr:cdsa {days=7}', function ($days) {
    SalesReportWork::initCDSA($days); //erp日销售额 (默认7天，倒推ERP销售额数据)
})->describe('saDaily');
Artisan::command('sr:dsa', function () {
    SalesReportWork::initDSA();     //erp近30天销售额
})->describe('saDaily');
Artisan::command('sr:msa', function () {
    SalesReportWork::initMSA();     //erp近14个月销售额
})->describe('saMonthly');

/**************************/
//统计平台报表(平台)(补录数据)
Artisan::command('sa:volume-back {startDate=null} {endDate=null}', function ($startDate,$endDate) {
    SalesVolume::initPlatformBack($startDate,$endDate); //平台日销售量，指定日期（开始时间，结束时间）
})->describe('volume');
Artisan::command('sa:amount-back {startDate=null} {endDate=null}', function ($startDate,$endDate) {
    SalesAmountDaily::initBack($startDate,$endDate); //平台日销售额，指定日期（开始时间，结束时间）
})->describe('amount');
Artisan::command('sa:amount-month-back {startDate=null}', function ($startDate) {
    SalesAmountMonthly::initBack($startDate); //平台月销售额，指定日期（开始时间）
})->describe('amountMonth');

//统计ERP报表(ERP)(补录数据)
Artisan::command('sr:cdsv-back {startDate=null} {endDate=null}', function ($startDate,$endDate) {
    DailySaleVolume::initSalesVolumeBack($startDate,$endDate); //erp日销售量，指定日期（开始时间，结束时间）
})->describe('csvcDaily');
Artisan::command('sr:cdsa-back {startDate=null} {endDate=null}', function ($startDate,$endDate) {
    DailySaleAmount::initBack($startDate,$endDate); //erp日销售额，指定日期（开始时间，结束时间）
})->describe('saDaily');
/**************************/


//产品毛利率
Artisan::command('profit-sku:sync', function () {
    SyncProfitSku::syncPS();
})->describe('syncProfitSku');
Artisan::command('profit-sku:seller', function () {
    SyncProfitSku::syncSeller();
})->describe('syncSeller');
Artisan::command('profit-sku:sale-price', function () {
    SyncProfitSku::syncSalePrice();
})->describe('syncSalePrice');
//更新产品毛利率表FBA费（每周2-6）
Artisan::command('profit-sku:fba-fee-daily', function () {
    SyncProfitSku::syncFBAFeeDaily();
})->describe('syncFBAFeeDaily');
//备货提货基础
Artisan::command('dss:sync', function () {
    DSyncSsBasic::syncSS();
})->describe('syncSS');
Artisan::command('dss:avg', function () {
    DSyncSsBasic::syncAVG();
})->describe('syncAVG');
Artisan::command('dss:basic', function () {
    DSyncSsBasic::initSS();
})->describe('initSSBasic');
//补备货提货数据
Artisan::command('dss:sync-init', function () {
    DSyncSsBasic::syncSSInit();
})->describe('syncSSInit');
//提货状态
Artisan::command('ssku:sync-status', function () {
    SyncSSku::status();
})->describe('syncSSkuStatus');
Artisan::command('stotal:sync-differ', function () {
    SyncSSku::totalDiffer();
})->describe('syncTotalDiffer');
//备货设置日期
Artisan::command('stocking:sync-set-date', function () {
    SyncStocking::syncSetDate();
})->describe('syncStockingSetDate');
Artisan::command('stocking:init-set-date', function () {
    SyncStocking::initSetDate();
})->describe('initStockingSetDate');
//更新月利润表-品类销售
Artisan::command('sr:sync-seller', function () {
    PaymentClassifyUp::exe();
})->describe('syncSeller');
//Weekly
//更新产品毛利率表FBA费
Artisan::command('profit-sku:fba-fee', function () {
    WSyncProfitSku::syncFBAFee();
})->describe('syncFBAFee');
//更新产品毛利率表退货率
Artisan::command('profit-sku:return', function () {
    WSyncProfitSku::syncReturn();
})->describe('syncReturn');
//抓取星级，大小类排名
Artisan::command('operation:crawl-star', function () {
    Crawl::initStar();
})->describe('crawlStar');
//Monthly
//更新产品毛利率表物流费
Artisan::command('profit-sku:logistics', function () {
    MSyncProfitSku::syncLogistics();
})->describe('syncLogistics');
//销量预估周期性更新
Artisan::command('ss:estimate', function () {
    MSyncSsBasic::syncEstimate();
})->describe('syncEstimate');
//定时清空日志记录
Artisan::command('logs:truncate', function () {
    TruncateTable::exe();
})->describe('truncateLogs');


/************* test start (非正式命令，请勿执行。)*********/
/***基础库****/
//测试,同步RakutenAPI订单至基础库
Artisan::command('test:get-rakuten-orders', function () {
    App\Logic\Plat\Rakuten\GrabOrders::getRakutenOrder();
})->describe('getRakutenOrders');
//测试,同步Rakuten订单至erp
Artisan::command('test:sync-rakuten-order', function () {
    App\Logic\Plat\Rakuten\GrabOrders::syncRakutenOrder();
})->describe('getRakutenOrders');
//测试,同步RakutenAPI订单至基础库
Artisan::command('test:get-rakuten-orders1', function () {
    App\Logic\Plat\Rakuten\GrabOrders::getRakutenOrder1();
})->describe('getRakutenOrders1');
//测试,同步RakutenAPI物流订单号
Artisan::command('test:sync-rakuten-orders-tracking-no', function () {
    App\Logic\Plat\Rakuten\GrabOrders::syncRakutenOrdersTrackingNo();
})->describe('syncRakutenOrdersTrackingNo');

//测试,同步ShopifyAPI订单至基础库
Artisan::command('test:get-shopify-orders', function () {
    App\Logic\Plat\Shopify\GrabOrders::getShopifyOrder();
})->describe('getShopifyOrders');
/***基础库****/


////补充一件代发库存（测试使用）
//Artisan::command('test:dropshipping-fnsku', function () {
//    \App\Logic\Test\Test::createDropshipping();
//})->describe('test');


//销售总表/备货总表 （sku、品类预估、sku平均值、sku预估、sku库存）
Artisan::command('test:update-product-stockup', function () {
    \App\Logic\Test\Test::updateProductStockup();
});

Artisan::command('test:sync-update-stockup', function () {
    App\Logic\ProductStockup\SyncStockup::syncUpdateStockup();
});

//更新产品状态
Artisan::command('test:update-product-sku-status', function () {
    \App\Logic\Test\Test::updateProductSkuStatus();
});


//同步库存总表（基础信息,SKU）
Artisan::command('test:sync-inventory-sku', function () {
    App\Logic\Inventory\SyncInventorySku::exe();
});
//同步库存总表（基础信息,站点sku）
Artisan::command('test:sync-inventory-area', function () {
    App\Logic\Inventory\SyncInventoryArea::exe();
});
//同步库存总表（基础信息,站点（库存fba））
Artisan::command('test:sync-inventory-area-fba-stock', function () {
    App\Logic\Inventory\SyncInventoryAreaFBAStock::exe();
});
//同步库存总表（基础信息,站点（海外仓/国内库存））
Artisan::command('test:sync-inventory-area-not-fba-stock', function () {
    App\Logic\Inventory\SyncInventoryAreaNotFBAStock::exe();
});


//test
Artisan::command('test:test', function () {
    \App\Logic\Test\Test::test();
    //\App\Logic\Test\Test::testRemovaleOrderDetail();
});

//test
Artisan::command('rakuten:test', function () {
    App\Logic\Plat\Rakuten\GrabOrders::syncBatchRakutenOrderTrackingNo();
});
Artisan::command('test:eu-stocking', function () {
    \App\Logic\Test\Test::testEuStocking();
});


Artisan::command('test:gucang-get-product-inventory', function () {
    App\Logic\Plat\Rakuten\Overseas\GuCang\Inventory\OverSeasInventory::getProductInventory();
});


/** **************************************************************************************************** **/
//获取Amazon订单(测试/console订单)
Artisan::command('test:console-amazon-order {deliveryOrderNo=null} {accountId=0}', function ($deliveryOrderNo,$accountId) {
    if ($deliveryOrderNo != 'null' && $accountId > 0) {
        $account = AmazonAccount::query()->find($accountId);
        if (!$account) dd('amazon account not exist!');

        $requiredConfig = RequiredConfig::getInit();
        $config = $requiredConfig->configuration($account->refresh_token, $account->aws_region);

        $apiInstance = new FbaCustomerOutboundApi($config);
        $fulfillmentOrder = CustomerOutbound::_getFulfillmentOrder($apiInstance,$deliveryOrderNo);
        dd($fulfillmentOrder);
    }
    dd('over!');
})->describe('console-amazon-order');

//获取Shopify订单(测试/console订单)
Artisan::command('test:console-shopify-order {deliveryOrderNo=null}', function ($deliveryOrderNo) {
    if ($deliveryOrderNo != 'null') {
        //Shopify订单ID
        $orderApiId = \App\Models\Shopify\ShopifyOrder::getValueByCode($deliveryOrderNo,'order_api_id');
        if (!$orderApiId) dd('shopify order number not exist!');

        $client = GrabCommon::getRequestClient('UNG');
        $shopifyOrder = $client->getOrderManager()->find($orderApiId);
        dd($shopifyOrder);
    }
    dd('over!');
})->describe('console-shopify-order');

//获取Rakuten订单(测试/console订单)
Artisan::command('test:console-rakuten-order {deliveryOrderNo=null}', function ($deliveryOrderNo) {
    if ($deliveryOrderNo != 'null') {
        $apiUrl = 'es/2.0/order/getOrder/'; //api方法
        $apiParams['version'] = 7; //版本
        $apiParams['orderNumberList'] = [$deliveryOrderNo];

        $rakutenOrder = RakutenClient::getRequest($apiUrl, $apiParams);
        dd($rakutenOrder);
    }
    dd('over!');
})->describe('console-rakuten-order');

//获取海外仓/谷仓订单(测试/console订单)
Artisan::command('test:console-gucang-order {deliveryOrderNo=null}', function ($deliveryOrderNo) {
    if ($deliveryOrderNo != 'null') {
        $overseasOrder = OverseasOrder::getReferenceOrder($deliveryOrderNo);
        dd($overseasOrder);
    }
    dd('over!');
})->describe('console-gucang-order');
/** **************************************************************************************************** **/
